package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/depts")
@RestController
public class DeptController {
    private static final Logger log = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门的信息
     * @return
     */
    @GetMapping
    public Result getAll()
    {
        List<Dept> list = deptService.list();
        log.info("查询了所有部门的信息");
        return Result.success(list);
    }

    @DeleteMapping
    public Result delete(Integer id)
    {
        deptService.delete(id);
        log.info("删除了部门id为{}的信息",id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Dept dept)
    {
        deptService.add(dept);
        log.info("添加了部门{}",dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        Dept dept = deptService.getInfo(id);
        log.info("查询了部门id为{}的信息",id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept)
    {
        deptService.update(dept);
        log.info("修改了部门{}",dept);
        return Result.success();
    }
}
